//============================================================================
// Name        : Index.h
// Author      : lucmir@dcc.ufmg.br
// Description :
//============================================================================

#ifndef __INDEX_H__
#define __INDEX_H__

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>

#include "Registry.h"
#include "Lexon.h"
#include "QueryResult.h"


#define UINT_SIZE		4			// in bytes
#define BUFFER_SIZE		500000			// in number of entries

typedef unsigned int IndexPosition;


class Index {

	std::ofstream ofs;
	std::ifstream ifs;

	unsigned int *buffer;
	unsigned int bufferPos;

	IndexPosition pos;		// position count (in bytes)
	IndexPosition initPos;	// initial position for the new term (in bytes)
	unsigned int lastTermId;	// last inserted term in the index

	unsigned int tf;			// term frequency
	Lexon *lexon;

	public:

		Index(Lexon *lexon);
		~Index();

		bool openToWrite(std::string fileName);
		void closeToWrite();
		bool openToRead(std::string fileName);
		void closeToRead();
		bool isOpenToWrite();
		bool isOpenToRead();

		void insert(Registry &registry);
		QueryResult *queryTerm(std::string);

		unsigned int getPos();
		Lexon *getLexon();

	private:

		void flushBuffer();

};

#endif
